#include <pthread.h>
#include <ctime>
#include <unistd.h>
#include "ringqueue.hpp"
#include "task.hpp"

using namespace std;

void *Consumer(void *args)
{
    RingQueue<Task> *rq = static_cast<RingQueue<Task> *>(args);
    while (true)
    {
        Task t;
        rq->Pop(&t);
        t.run();
        cout << "消费了一个数据" << t.GetRet() << endl;
    }
    return nullptr;
}

void *Productor(void *args)
{
    RingQueue<Task> *rq = static_cast<RingQueue<Task> *>(args);
    int len = opers.size();
    while (true)
    {
        int data1 = rand() % 10 + 1;
        char oper = opers[rand() % len];
        int data2 = rand() % 10;
        Task t(data1, data2, oper);
        rq->Push(t);
        cout << "生产了一个数据" << t.Getinfo() << endl;
        sleep(1);
    }
    return nullptr;

}

int main()
{
    srand(time(0));
    RingQueue<Task> *rq = new RingQueue<Task>();
    pthread_t c[3], p[5];
    for (int i = 0; i < 3; i++)
    {
        pthread_create(c + i, nullptr, Consumer, rq);
    }
    for (int i = 0; i < 5; i++)
    {
        pthread_create(p + i, nullptr, Productor, rq);
    }

    for (int i = 0; i < 3; i++)
    {
        pthread_join(c[i], nullptr);
    }
    for (int i = 0; i < 5; i++)
    {
        pthread_join(p[i], nullptr);
    }
    return 0;
}